package com.ebelter.bpm.activitys;


import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.ebelter.bpm.R;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CombinView1;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class BPM_ModifyNameActivity extends BaseActivity {

    public static final String TAG = "BPM_ModifyNameActivity";
    public static final int BPM_ModifyNameActivity_REQUEST_CODE = 100;


    TextView topTitleTv;

    CombinView1 cb1_bpm_name;


    private String oriName;//原始的名字




    @Override
    protected int loadLayoutById() {
        return R.layout.activity_bpm_modify_name;
    }



    @Override
    protected void initView() {
        FV();



    }

    @Override
    protected void initData() {
        oriName = UserSpUtil.readString(Constants.IUser.NICK_NAME);
        cb1_bpm_name.setTitle1(oriName);
        cb1_bpm_name.setSelection(oriName.length());//将光标移至文字末尾

    }

    private void FV() {
        topTitleTv=findViewById(R.id.top_title_tv);
        cb1_bpm_name = findViewById(R.id.cb1_bpm_name);


    }



    //返回键被按下
    public void backClick(View view){
        LogUtils.i(TAG,"返回键被按下");
        finish();
    }



    //完成按钮
    public void doneBtClick(View view){
        LogUtils.i(TAG,"doneBtClick");

        String newName=cb1_bpm_name.getTitle1();

        if (TextUtils.isEmpty(newName)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }

        if (newName.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        if (TextUtils.equals(newName,oriName)){
            ToastUtil.show(R.string.Modify_Success);
            setResult(resultCodeFailure);
            finish();
            return;
        }


        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        final Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.nickName,newName);

        NetUtils.getInstance().updateInfo(this, paramMap, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }
                if (baseResponse.resultCode==0){
                    ToastUtil.show(R.string.Modify_Success);
                    UserSpUtil.updateLocalData(paramMap);
                    setResult(resultCodeSuccess);
                    finish();

                }else if (baseResponse.resultCode==112){
                    ToastUtil.show(R.string.nickname_have_been_registered);
                }else if (baseResponse.resultCode==110){
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }else {
                    ToastUtil.show(R.string.Modify_Failed);
                }

            }
        });





    }

}
